<?php
namespace Unicom\Controller;
use Think\Controller;

class ConfigController extends AdminController {
	public function syncConfig() {
		layout(false);
		$model = M('cp.config', 'cp_');
		$no_sync = $model->where(array('key' => 'no_sync'))->find();
		$wx_sync = $model->where(array('key' => 'wx_sync'))->find();
		$category = I('param.category', 0);
		if (IS_GET) {
			$data = $model->where(array('category' => $category))->select();
            $tmp=array();
			foreach ($data as $key => &$value) {
				$arr = array();

				if (empty($value['value'])) {



				} else {
					$arr = explode(',', $value['value']);
				}

				$select = array();
				for ($i = 0; $i < 50; $i++) {

					if (in_array($i, $arr)) {
						$select[$i] = array('index' => $i, 'selected' => true);
					} else {
						$select[$i] = array('index' => $i, 'selected' => false);
					}
				}

				$count = sizeof($arr);
				$percent = $count * 0.2 * 10;

				$value['percent'] = $percent;
				$value['select'] = $select;
				$tmp[$value['key']]=$value;
			}

			$this->assign('data', $tmp);
            $this->assign('tmp', json_encode($tmp));
            $this->assign('ttt', "hhhhh");

			$this->display();

		} else {
			//var_dump($_REQUEST);
			$ret = array();
			$servcode = I('param.servcode', '', 'string');
			$value = I('param.value', '', 'string');
			if ($model->where(array('key' => $servcode))->setField('value', $value)) {
				$ret['status'] = 1;
				$ret['info'] = '设置成功' . $servcode . '->' . $value;
			} else {
				$ret['status'] = 0;
				$ret['info'] = '设置失败' . $servcode . '->' . $value;
			}

			$this->ajaxReturn($ret);

		}

	}

	//炫铃扣设置
	public function ringConfig() {
		layout(false);
		$model = M('cp.config', 'cp_');

		if (IS_GET) {
			$data = $model->where(array('category' => 1))->select();

			foreach ($data as $key => &$value) {
				$arr = array();

				if (empty($value['value'])) {

				} else {
					$arr = explode(',', $value['value']);
				}

				$select = array();
				for ($i = 0; $i < 50; $i++) {

					if (in_array($i, $arr)) {
						$select[$i] = array('index' => $i, 'selected' => true);
					} else {
						$select[$i] = array('index' => $i, 'selected' => false);
					}
				}

				$count = sizeof($arr);
				$percent = $count * 0.2 * 10;

				$value['percent'] = $percent;
				$value['select'] = $select;
			}

			$this->assign('data', $data);
			$this->display();

		} else {
			//var_dump($_REQUEST);
			$ret = array();
			$servcode = I('param.servcode', '', 'string');
			$value = I('param.value', '', 'string');
			if ($model->where(array('key' => $servcode))->setField('value', $value)) {
				$ret['status'] = 1;
				$ret['info'] = '设置成功' . $servcode . '->' . $value;
			} else {
				$ret['status'] = 0;
				$ret['info'] = '设置失败' . $servcode . '->' . $value;
			}

			$this->ajaxReturn($ret);

		}

	}

	/**
	 * 黑名单
	 * @return [type] [description]
	 */
	public function blacklistConfig() {
		layout(false);
		$model = M('cp.blacklist', 'cp_');
		if (IS_GET) {
			$count = $model->count();
			$listRows = 10;
			if (isset($REQUEST['r'])) {
				$listRows = (int) $REQUEST['r'];
			} else {
				$listRows = C('LIST_ROWS') > 0 ? C('LIST_ROWS') : 10;
			}
			$Page = new \Think\Page($count, $listRows); // 实例化分页类 传入总记录数和每页显示的记录数
			$show = $Page->show(); // 分页显示输出

			$data = $model->order(' create_time desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
			$this->assign('data', $data);
			$this->assign('page', $show);
			$this->display();

		} else {

			if ($_FILES['blacklist']['error'] == 0) {
				$txt = $_FILES['blacklist']['tmp_name'];

				if ("text/plain" == $_FILES['blacklist']['type']) {

					$content = file_get_contents($txt);

					$arr = explode("\n", $content);

					foreach ($arr as $key => $value) {
						$where = array('phone' => cutstr_html($value));
						$item = array('phone' => cutstr_html($value), 'create_time' => time());
						$ret = $model->where($where)->find();

						if (!$ret) {
							$model->add($item);

						}

					}

					$this->success('导入完成');

				} else {
					$this->error("不支持此类文件上传");
				}
			} else {
				$this->error(showUploadError($_FILES['blacklist']['error']));
			}

		}

	}

	/**
	 * 删除黑名单
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public function removeBlackListByID($id) {
		$model = M('cp.blacklist', 'cp_');
		if ($model->where(array('id' => $id))->delete()) {
			$this->success('删除成功');
		} else {
			$this->error('出错');
		}

	}

	/**
	 * 省份管理
	 * @return [type] [description]
	 */
	public function provinceConfig() {
		layout(false);
		$model = M('cp.privence', 'cp_');
		if (IS_GET) {
			$count = $model->count();
			$listRows = 15;
			if (isset($REQUEST['r'])) {
				$listRows = (int) $REQUEST['r'];
			} else {
				$listRows = C('LIST_ROWS') > 0 ? C('LIST_ROWS') : 15;
			}
			$Page = new \Think\Page($count, $listRows); // 实例化分页类 传入总记录数和每页显示的记录数
			$show = $Page->show(); // 分页显示输出

			$data = $model->limit($Page->firstRow . ',' . $Page->listRows)->select();
			$this->assign('data', $data);
			$this->assign('page', $show);
			$this->display();
		} else {
			set_time_limit(180000);
			if ($_FILES['province']['error'] == 0) {
				$excel = $_FILES['province']['tmp_name'];
				if (readExcel($excel)) {
					$this->success('导入完成');
				} else {
					$this->error('导入失败');
				}
				// $arr = explode("\n",$content);
				// foreach ($arr as $key => $value) {
				// 	if(!($model->where(array('phone'=>$value))->find())){
				// 		$model->add(array('phone'=>$value));
				// 	}

				// }

				// $this->success('导入完成');

			} else {
				$this->error(showUploadError($_FILES['province']['error']));
			}

		}

	}

	/**
	 * 同步接口配置
	 * @return [type] [description]
	 */
	public function syncInterfaceConfig() {
		layout(false);
		if (IS_GET) {
			$model = M('cp.syncinterface', 'cp_');
			$data = $model->select();
			$this->assign('data', $data);
			$this->display();
		}
	}

	/**
	 * 新增同步接口
	 */
	public function addSyncInterface() {
		layout(false);
		if (IS_GET) {
			$this->display();
		} else {
			$model = M('cp.syncinterface', 'cp_');
			$data = $model->create();
			$ret = array();
			if ($data) {
				if ($model->where(array('servcode' => $data['servcode']))->find()) {
					$ret['status'] = 1;
					$ret['info'] = "当前指令已经配置";
				} else {
					if ($model->add()) {
						$ret['status'] = 0;
						$ret['info'] = "配置成功";
					} else {
						$ret['status'] = 1;
					}

					$ret['info'] = "错误:" . $model->getDbError();
				}

			} else {
				$ret['status'] = 1;
				$ret['info'] = "错误:" . $model->getError();
			}

			$this->ajaxReturn($ret);

		}

	}

	/**
	 * 删除同步地址
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public function deleteSyncInterface($id) {
		layout(false);
		$model = M('cp.syncinterface', 'cp_');
		$ret = array();
		if ($model->where(array('id' => $id))->delete()) {
			$ret['status'] = 0;
			$ret['info'] = '删除成功!';
		} else {
			$ret['status'] = 1;
			$ret['info'] = '删除失败!';
		}
		$this->ajaxReturn($ret);

	}


	public function setChannelkou(){
	    $ch = I('param.channel',0);
	    $kou=I('param.kou','');

	    $ret["status"]=0;
	    $ret['ch']=$ch;
	    $ret['kou']=$kou;
        $this->ajaxReturn($ret);

    }

    public function province_allow(){
	    layout(false);
	    $model=M('cp.allow_provence','cp_');
	    if (IS_GET){
            $data = $model->select();
            //var_dump($data);
            $this->assign('data',$data);
            $this->display();
        }else{
            $data=$model->create($_POST);
            $data->add();
            var_dump($data);
        }

    }

}